<template>
    <div class="cmt-container">
        <h3>发表评论</h3>
        <hr>
        <textarea placeholder="请输入评论的内容(最多入槽120个字)" v-model="msg"></textarea>
        <mt-button type="primary" size="large" @click="postComment"> 发表评论 </mt-button>
        <div class="cmt-list">
            <div class="cmt-item" v-for="(item, index) in comments" :key="index">
            <!-- <div class="cmt-item" v-for="(item, index) in comments.slice(0, 5)" :key="index"> -->
            <!-- <div class="cmt-item" v-for="(item, index) in comments" v-if="index < 10" :key="index"> -->
                <div class="cmt-title"> 
                    第 {{ index + 1 }} 楼&nbsp;{{ item.user }}&nbsp;&nbsp;发表时间：{{ item.createDate | dateFormat }}
                </div>
                <div class="cmt-body"> 
                    {{ item.content }}
                </div>
            </div>
        </div>

        <mt-button type="danger" size="large" plain @click="getMore"> 加载更多 </mt-button>
    </div>
</template>

<script>
import { Toast } from "mint-ui"

export default {
    data(){
        return {
            endNum: 5,
            comments: [],
            msg:"" // 评论内容
        }
    },
    created(){
        this.getComments()
    },
    methods:{
        getComments(){
            console.log('id==== ' + this.id)
            this.$http.get('getComments/' + this.id + '/' + this.endNum ).then(result => {
                if(result.body.status == 'SUCCESS'){
                    this.comments = result.body.message
                    console.log(this.comments)
                }else{
                    Toast("获取评论失败")
                }
            })

        },
        getMore(){ // 加载更多
            this.endNum = this.endNum + 5
            this.getComments()
        },
        postComment(){
            if(this.msg.trim().length == 0){
                return Toast("评论内容不能为空！")
            }
            // 保存评论内容
           console.log( "msg="+this.msg + "--id=" + this.id + "----")
           // 追加数据
           var cmt = {
                content: this.msg,
                createDate: Date.now(),
                user: 'nomore person'
           }
           this.comments.unshift(cmt)
           this.msg = ''
        }
    },
    props:["id"]
}
</script>

<style lang="scss" scoped>
.cmt-container{
    h3{
        font-size: 16px;
    }
    textarea{
        font-size: 14px;
        height: 85px;
        margin: 0;
    }
    .cmt-list{
        margin: 5px 0;
        .cmt-item{
            font-size: 13px;
            .cmt-title{
                background-color: #ccc;
                line-height: 30px;
            }
            .cmt-body{
                line-height: 35px;
                text-indent: 2em;
            }
        }
        
    }
}
</style>